package com.market.service.wx.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.market.bean.MarketTopic;
import com.market.bean.MarketTopicExample;
import com.market.bean.common.BasePageInfo;
import com.market.bean.common.CommonData;
import com.market.mapper.MarketTopicMapper;
import com.market.service.wx.WxTopicService;
import com.market.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wh194@foxmail.com
 * @since 2023/01/01 17:37
 */
@Service
public class WxTopicServiceImpl implements WxTopicService {
    @Autowired
    MarketTopicMapper MarketTopicMapper;

    /**
     * 小程序专题列表
     * @param info
     * @param title
     * @param subtitle
     * @return
     */
    @Override
    public CommonData<MarketTopic> list(BasePageInfo info, String title, String subtitle) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketTopicExample example = new MarketTopicExample();

        if (info.getSort()!=null){
            example.setOrderByClause(info.getSort() + " " + info.getOrder());
        }

        MarketTopicExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(title)) {
            criteria.andTitleLike("%" + title + "%");
        }
        if (!StringUtil.isEmpty(subtitle)) {
            criteria.andSubtitleLike("%" + subtitle + "%");
        }
        criteria.andDeletedEqualTo(false);
        List<MarketTopic> list = MarketTopicMapper.selectByExampleWithBLOBs(example);
        PageInfo<MarketTopic> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    /**
     * 小程序专题详情
     * @param id
     * @return
     */
    @Override
    public MarketTopic detail(Integer id) {
        MarketTopic marketTopic = MarketTopicMapper.selectByPrimaryKey(id);
        return marketTopic;
    }

    /**
     * 专题相关
     * @param id
     * @return
     */
    @Override
    public MarketTopic related(Integer id) {
        MarketTopic marketTopic = MarketTopicMapper.selectByPrimaryKey(id);
        return marketTopic;
    }

}
